package org.sword.wechat4j.driver;

import org.sword.wechat4j.type.MsgType;
import org.sword.wechat4j.request.*;
import org.sword.wechat4j.response.WechatResponse;

/**
 * 消息驱动
 * Created by sean on 2016-07-22-0022.
 */
public abstract class MessageDriver extends MessageDriverI {

    @Override
    public final WechatResponse service(BaseMessage baseMessage) {
        WechatResponse wechatResponse = super.service(baseMessage);
        distributeMessage(baseMessage);
        return wechatResponse;
    }

    /**
     * 消息事件分发
     */
    private void distributeMessage(BaseMessage message) {
        MsgType msgType = MsgType.valueOf(message.getMsgType());
        switch (msgType) {
            case event:
                break;
            case text:
                onText((TextMessage) message);
                break;
            case image:
                onImage((PicMediaMessage) message);
                break;
            case location:
                onLocation((LocationMessage) message);
                break;
            case link:
                onLink((LinkMessage) message);
                break;
            case video:
                onVideo((VideoMediaMessage) message);
                break;
            case voice:
                onVoice((VoiceMediaMessage) message);
                break;
            case shortvideo:
                onShortVideo((VideoMediaMessage) message);
                break;
            default:
                onUnknown(message);
                break;
        }
    }

    /**
     * 文本消息处理
     * @param message 消息
     */
    protected abstract void onText(final TextMessage message);

    /**
     * 图像
     * @param message 消息
     */
    protected abstract void onImage(final PicMediaMessage message);

    /**
     * location消息处理msgtype=location
     * @param message 消息
     */
    protected abstract void onLocation(final LocationMessage message);

    /**
     * link消息处理
     * @param message 消息
     */
    protected abstract void onLink(final LinkMessage message);

    /**
     * shortvideo消息处理
     * @param message 消息
     */
    protected abstract void onShortVideo(final VideoMediaMessage message);

    /**
     * voice消息处理
     * @param message 消息
     */
    protected abstract void onVoice(final VoiceMediaMessage message);

    /**
     * video消息处理
     * @param message 消息
     */
    protected abstract void onVideo(final VideoMediaMessage message);

    /**
     * 未知消息类型的错误处理逻辑，不需要处理则空方法即可
     * @param message 消息
     */
    protected abstract void onUnknown(BaseMessage message);
}
